package com.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.db.common.annotation.RequiredRole;
import com.db.common.pojo.PageObject;
import com.db.dao.SysRoleDao;
import com.db.dao.SysRoleMenuDao;
import com.db.pojo.SysRole;
import com.db.pojo.SysRoleMenu;
import com.db.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class SysRoleServiceImpl implements SysRoleService {

	@Autowired
	private SysRoleDao sysRoleDao;

	@Autowired
	private SysRoleMenuDao sysRoleMenuDao;

	@RequiredRole(operation = "查询角色信息")
	@Transactional(readOnly = true)
	@Override
	public PageObject<SysRole> findObjects(String username, Integer pageCurrent) {

		QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
		wrapper.orderByDesc("modifiedTime");
		if (!StringUtils.isEmptyOrWhitespace(username)) {
			wrapper.like("username", username);
		}

		IPage<SysRole> iPage = new Page<>(pageCurrent, 3);
		IPage<SysRole> page = sysRoleDao.selectPage(iPage, wrapper);

		return new PageObject<SysRole>(page.getCurrent(), (int) page.getSize(), page.getTotal(), page.getPages(), page.getRecords());
	}

	@RequiredRole(operation = "删除角色")
	@Override
	public void deleteById(Integer id) {
		sysRoleDao.deleteById(id);
	}

	@RequiredRole(operation = "新增角色")
	@Override
	public void saveObject(SysRole sysRole, Integer[] menuIds) {
		sysRoleDao.insert(sysRole); //角色自身新增数据

		sysRoleMenuDao.insertObject(sysRole.getId(), menuIds); //角色和菜单中间表新增数据
	}

	@RequiredRole(operation = "查询单个角色信息")
	@Override
	public Map<String, Object> findById(Integer id) {

		SysRole sysRole = sysRoleDao.selectById(id);
		QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
		wrapper.eq("role_id", id);
		List<SysRoleMenu> sysRoleMenus = sysRoleMenuDao.selectList(wrapper);
		Integer[] ins = new Integer[sysRoleMenus.size()];

		for (int i = 0; i < ins.length; i++) {
			ins[i] = sysRoleMenus.get(i).getMenu_id();
		}

		Map<String, Object> map = new HashMap<>();
		map.put("id", sysRole.getId());
		map.put("name", sysRole.getName());
		map.put("note", sysRole.getNote());
		map.put("menuIds", ins);
		return map;
	}

	@RequiredRole(operation = "更新角色信息")
	@Override
	public void updateObjects(SysRole sysRole, Integer[] menuIds) {
		sysRoleDao.updateById(sysRole);

		/* 删除原有关系表 */
		QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
		wrapper.eq("role_id", sysRole.getId());
		sysRoleMenuDao.delete(wrapper);
		/* 添加新关系表 */
		sysRoleMenuDao.insertObject(sysRole.getId(), menuIds);
	}

	@Override
	public List<SysRole> findRoleIdName() {

		List<SysRole> sysRoles = sysRoleDao.selectList(null);
		return sysRoles;
	}
}
